package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.HealthRecord;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.SkillCertificate;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.healthRecord.*;
import com.battle.pojo.vo.healthRecord.*;
import com.battle.pojo.dto.healthRecord.*;
import com.battle.service.check.EncryptionUtil;
import com.battle.service.repository.in.HealthRecordRepository;
import com.battle.service.repository.in.PersonWarCaseRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.HealthRecordService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：健康档案服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class HealthRecordServiceImpl implements HealthRecordService {

    private final HealthRecordRepository healthRecordRepository;
    private final PersonWarCaseRepository personWarCaseRepository;
    private final UserRepository userRepository;
    private final EncryptionUtil encryptionUtil;

    public HealthRecordServiceImpl(HealthRecordRepository healthRecordRepository, PersonWarCaseRepository personWarCaseRepository, UserRepository userRepository, EncryptionUtil encryptionUtil) {
        this.healthRecordRepository = healthRecordRepository;
        this.personWarCaseRepository = personWarCaseRepository;
        this.userRepository = userRepository;
        this.encryptionUtil = encryptionUtil;
    }


    /**
     * 创建健康档案
     *
     * @param param
     */
    @Override
    public void createHealthRecord(CreateHealthRecordParam param) {
        HealthRecord healthRecord = new HealthRecord();
        BeanUtils.copyProperties(param, healthRecord);
        Long id = IdGenerator.SNOW.generate();
        healthRecord.setId(id);
        //hmac加密
        if (param.getFileUrl() != null) {
            String hmac = (String) encryptionUtil.encryptionByHmac(param.getFileUrl());
            healthRecord.setFileUrlHmac(hmac);
        }
        healthRecordRepository.create(healthRecord);
    }

    /**
     * 修改健康档案
     *
     * @param param
     */
    @Override
    public void editHealthRecord(EditHealthRecordParam param) {
        Long id = param.getId();
        HealthRecord healthRecord = healthRecordRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, healthRecord);
        //hmac加密
        if (param.getFileUrl() != null) {
            String hmac = (String) encryptionUtil.encryptionByHmac(param.getFileUrl());
            healthRecord.setFileUrlHmac(hmac);
        }
        healthRecordRepository.edit(healthRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bathUpdate() {
        List<HealthRecord> healthRecords = healthRecordRepository.getBaseMapper().selectList(new LambdaQueryWrapper<>());
        if (CollectionUtil.isNotEmpty(healthRecords)) {
            for (HealthRecord healthRecord : healthRecords) {
                healthRecordRepository.updateById(healthRecord);
            }
        }
    }

    /**
     * 移除健康档案
     *
     * @param id
     */
    @Override
    public void removeHealthRecord(Long id) {
        healthRecordRepository.removeById(id);
    }

    /**
     * 启动/禁用健康档案
     *
     * @param id
     */
    @Override
    public void enabledHealthRecord(Long id) {
        HealthRecord healthRecord = healthRecordRepository.getByIdCheck(id);
        healthRecord.setIsDisable(!healthRecord.getIsDisable());
        healthRecordRepository.edit(healthRecord);
    }

    /**
     * 获取健康档案详情
     *
     * @param id
     * @return
     */
    @Override
    public GetHealthRecordVo getHealthRecord(Long id) {
        HealthRecord healthRecord = healthRecordRepository.getByIdCheck(id);
        GetHealthRecordVo vo = new GetHealthRecordVo();
        BeanUtils.copyProperties(healthRecord, vo);
        return vo;
    }

    /**
     * 获取健康档案 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetHealthRecordListVo> getHealthRecordList(GetHealthRecordListParam param) {

        SelectHealthRecordDto dto = new SelectHealthRecordDto();
        BeanUtils.copyProperties(param, dto);
        Page<HealthRecord> page = healthRecordRepository.selectHealthRecord(dto);
        List<HealthRecord> healthRecordList = page.getRecords();
        List<GetHealthRecordListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(healthRecordList)) {
            List<Long> userIds = new ArrayList<>();
            healthRecordList.forEach(v -> {
                userIds.add(v.getCreator());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);
            for (HealthRecord healthRecord : healthRecordList) {
                GetHealthRecordListVo vo = new GetHealthRecordListVo();
                BeanUtils.copyProperties(healthRecord, vo);
                vo.setCreateTime(healthRecord.getCreateTime());

                User user = userMap.get(healthRecord.getCreator());
                if (user != null) {
                    vo.setCreateName(user.getName());
                }
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取健康档案下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetHealthRecordDownListVo> getHealthRecordDownList(GetHealthRecordDownListParam param) {
        HealthRecord po = new HealthRecord();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<HealthRecord> healthRecordList = healthRecordRepository.getListByEntity(po);

        List<GetHealthRecordDownListVo> list = new ArrayList<>();
        for (HealthRecord healthRecord : healthRecordList) {
            GetHealthRecordDownListVo vo = new GetHealthRecordDownListVo();
            BeanUtils.copyProperties(healthRecord, vo);
            list.add(vo);
        }
        return list;
    }


}



